export const TOKEN_KEY = 'access_token'
export const USER_INFO_KEY = 'user_info'
export const EXPIRE_TIME_KEY = 'expires_time'
export const REFRESH_TOKEN_KEY = 'refresh_token'
import http from "./http";

// 获取token
let refreshPromise: Promise<string | null> | null = null;
let lastRefreshFailedAt: number | null = null;

export const getToken = () => {
    return uni.getStorageSync(TOKEN_KEY);
};

export const postRefreshToken = async (refreshToken: any) => {
    return await http.post(`/member/auth/refresh-token?refreshToken=${refreshToken}`)
}

function isExpire(expiresTime: number) {
    return expiresTime < Date.now()
}

// 设置token
export const setToken = (token) => {
    return uni.setStorageSync(TOKEN_KEY, token)
}

// 移除token
export const removeToken = () => {
    return uni.removeStorageSync(TOKEN_KEY)
}

// 获取用户信息
export const getUserInfo = () => {
    return uni.getStorageSync(USER_INFO_KEY)
}

// 设置用户信息
export const setUserInfo = (userInfo) => {
    return uni.setStorageSync(USER_INFO_KEY, userInfo)
}

// 移除用户信息
export const removeUserInfo = () => {
    return uni.removeStorageSync(USER_INFO_KEY)
}

export const getRefreshToken = () => {
    return uni.getStorageSync(REFRESH_TOKEN_KEY)
}
// 清空所有存储
export const clearStorage = () => {
    uni.removeStorageSync(TOKEN_KEY)
    uni.removeStorageSync(USER_INFO_KEY)
    uni.removeStorageSync(EXPIRE_TIME_KEY)
    uni.removeStorageSync(REFRESH_TOKEN_KEY)
}